package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.New.NewsDetailsActivity;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.GoodlistBean;
import com.google.gson.Gson;

import java.util.List;

import takeout.home.ProductsettlementActivity;

public class DeliverygoodslistApter extends RecyclerView.Adapter<DeliverygoodslistApter.MyHolder>{
    private Context context;
    private List<GoodlistBean.DataDTO> list;
    private Gson gson;

    public DeliverygoodslistApter(Context context, List<GoodlistBean.DataDTO> list) {
        this.context = context;
        this.list = list;
        gson = new Gson();
    }

    @NonNull
    @Override
    public DeliverygoodslistApter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goodlist,null);
        return new DeliverygoodslistApter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DeliverygoodslistApter.MyHolder holder, int position) {
        holder.addressDetail.setText(list.get(position).getAddressDetail());
        holder.phone.setText(list.get(position).getPhone());
        holder.name.setText(list.get(position).getName());
        holder.re_goodsdislist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*Intent intent = new Intent(context, ProductsettlementActivity.class);
                gson = new Gson();
                String JSON = gson.toJson(list.get(position));
                intent.putExtra(Commom.PRODUCTSETTLEMENTACTIVITYID,JSON);
                context.startActivity(intent);
                dataDTO = list.get(position);*/
               // GoodlistBean.DataDTO dataDTO = list.get(position);

                //获取地址，包装起来
                String backData = gson.toJson(list.get(position));
                //弹窗消失，Activity接受数据
               if(context instanceof ProductsettlementActivity ){
                   ProductsettlementActivity activity = (ProductsettlementActivity)context;
                   activity.dismissDialog(backData);
               }
                //在前一个Activity显示对应的数据
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView addressDetail;
        TextView phone;
        TextView name;
        ImageView update;
        RelativeLayout re_goodsdislist;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            addressDetail = itemView.findViewById(R.id.addressDetail);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
            re_goodsdislist = itemView.findViewById(R.id.re_goodsdislist);
            update = itemView.findViewById(R.id.update);
        }
    }
}



